package com.example.performance.jol.official;

import org.openjdk.jol.info.GraphLayout;
import org.openjdk.jol.vm.VM;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Aleksey Shipilev
 */
@SuppressWarnings({"java:S101", "java:S106", "java:S1215"})
public class JOLSample_25_Compaction {

    /*
     * This is the example how VM compacts the objects.
     *
     * This example generates PNG images in your current directory.
     *
     * You can see the freshly allocated and populated list has quite
     * the sparse layout. It happens because many temporary objects are
     * allocated while populating the list. The subsequent GCs compact
     * the list into the one or few dense blocks.
     *
     * Run this test with -Xmx1g -XX:+UseParallelGC -XX:ParallelGCThreads=1
     * for best results.
     */

    public static volatile Object sink;

    public static void main(String[] args) throws Exception {
        System.out.println(VM.current().details());

        // allocate some objects to beef up generations
        for (int c = 0; c < 1000000; c++) {
            sink = new Object();
        }

        System.gc();

        List<String> list = new ArrayList<>();
        for (int c = 0; c < 1000; c++) {
            list.add("Key" + c);
        }

        for (int c = 1; c <= 10; c++) {
            GraphLayout.parseInstance(list).toImage("list-" + c + ".png");
            System.gc();
        }
    }


}
